<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>web_socket</title>
    <script src="/static/aaa_example/js/jquery_3_2_0.js"></script>
    <script src="/static/aaa_example/js/socket.io_4_0_1.js"></script>

    <link href="/static/aaa_example/css/index.css" rel="stylesheet">

    <script>
        // sock
        var ws = new WebSocket("ws://127.0.0.1:5000/example/Sock");
        ws.onmessage = function (event) {
            // content = document.createTextNode(event.data);
            // $("#WebSocket").html(content);
            document.getElementById('WebSocket').innerText = event.data
            // ws.send("ok2");
            // ws.close();
        };
        ws.onopen = function (event) {
            ws.send("ok1");
        }
        ws.onerror = function (event) {
            console.log(event);
            ws.close();
        }
        ws.onclose = function (event) {
            console.log(event);
            ws.close();
        }
        function sendmsg() {
            console.log("sendmsg click");
            ws.send("-1");
        }
        function closews() {
            console.log("closews click");
            ws.close();
        }
    
        // socketio
        var socket = io.connect('http://' + document.domain + ':' + location.port + "/example/socketio");
    
        socket.on('connect', function () {
            console.log('connect');
        });
    
        socket.on('disconnect', function () {
            console.log('disconnect');
        });
    
        socket.on('message', function (msg) {
            console.log('Received message:', msg);
        });
    
        function joinRoom() {
            var username = document.getElementById("username").value;
            var room = document.getElementById("room").value;
            socket.emit("join", { username: username, room: room });
        }
    
        function leaveRoom() {
            var username = document.getElementById("username").value;
            var room = document.getElementById("room").value;
            socket.emit("leave", { username: username, room: room });
        }
    
        socket.on("join_room", function (message) {
            var div = document.createElement("div");
            div.textContent = message;
            document.getElementById("messages").appendChild(div);
        });
    
        socket.on("leave_room", function (message) {
            var div = document.createElement("div");
            div.textContent = message;
            document.getElementById("messages").appendChild(div);
        });
    
        function sendMessage() {
            var message = prompt('Enter your message:');
            socket.emit('message', message);
        }
        function closeio() {
            socket.disconnect();
            // socket.close()
        }
    </script>
</head>

<body>
    <div>
        <button onclick="sendmsg()">发送消息</button>
        <button onclick="closews()">关闭websocket</button>
        <div id='WebSocket'></div>
    </div>
    <div id="room-form">
        <input type="text" id="username" placeholder="Username">
        <input type="text" id="room" placeholder="Room">
        <button onclick="joinRoom()">Join Room</button>
        <button onclick="leaveRoom()">Leave Room</button>
        <button onclick="sendMessage()">sendMessage</button>
        <button onclick="closeio()">closeio</button>
        <div id="messages"></div>
    </div>
</body>

</html>